﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Net.NetworkInformation;
using Microsoft.Phone.Shell;
using PoznanEvents.Models;

namespace PoznanEvents
{
    public partial class MainPage : PhoneApplicationPage
    {
        public ProgressIndicator LoadingBar = null;
        private class State
        {
            public CultureInfo Result { get; set; }
        }
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            if (LoadingBar == null)
            {
                LoadingBar = new ProgressIndicator()
                {
                    IsVisible = false,
                    IsIndeterminate = false
                };
            }

            SystemTray.SetProgressIndicator(this, LoadingBar);

            if (NetworkInterface.GetIsNetworkAvailable())
            {
                if (!App.ViewModel.IsDataLoaded)
                    SetLoadingBar(this, new EventArgs());

                App.ViewModel.SetLoadingBar += SetLoadingBar;
                App.ViewModel.CloseLoadingBar += CloseLoadingBar;
                DataContext = App.ViewModel;
            }



        }

        private void NavigateToEventView(Event item)
        {
            var page = "/EventPage.xaml";

            if (item.EventLongitue > 0 || item.EventLatitude > 0)
                page = "/EventPageWithMap.xaml";

            NavigationService.Navigate(new Uri(page, UriKind.Relative));
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/SettingPage.xaml", UriKind.Relative));
        }

        private void TodayEvents_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var item = (Event)TodayEvents.SelectedItem;
            if (item != null)
            {
                App.ViewModel.SelectedEvent = item;

                NavigateToEventView(item);
            }
        }

        private void ThreeDayEvent_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var item = (Event)ThreeDayEvents.SelectedItem;
            if (item != null)
            {
                App.ViewModel.SelectedEvent = item;

                NavigateToEventView(item);
            }
        }

        private void WeekEvents_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var item = (Event)WeekEvents.SelectedItem;
            if (item != null)
            {
                App.ViewModel.SelectedEvent = item;

                NavigateToEventView(item);
            }
        }

        private void RefreshBarIconButton_Click(object sender, EventArgs e)
        {
            App.ViewModel.ReLoadData();
        }

        public void SetLoadingBar(object sender, EventArgs args)
        {
            LoadingBar.IsVisible = true;
            LoadingProgressBar.Visibility = Visibility.Visible;
        }

        public void CloseLoadingBar(object sender, EventArgs args)
        {
            LoadingBar.IsVisible = false;
            LoadingProgressBar.Visibility = Visibility.Collapsed;
        }
    }
}